package com.huluobo.jetpackdemo.flow

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.huluobo.jetpackdemo.R
import com.huluobo.jetpackdemo.hilt.UserBean
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch

class FlowActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_flow)
        CoroutineScope(Job()).launch {
            //接收数据
            loadData2().collect {
                Log.i("TagA", "int:$it")
            }
        }
    }

    private fun loadData() = flow {
        Log.i("TagA", "进入load方法")
        for (i in 1..10) {
            kotlinx.coroutines.delay(1000)
            emit(i)
        }
    }

    private fun loadData2() = flow {
        for (i in 1..20) {
            emit(i)
            kotlinx.coroutines.delay(1000)
        }
    }.filter {
        it % 2 != 0
    }.map {
        //转化,映射
        UserBean("LC", "123", it)
    }

}